package com.atguigu.userprofile.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.userprofile.bean.TagCondition;
import com.atguigu.userprofile.bean.TaskInfo;
import com.atguigu.userprofile.bean.UserGroup;
import com.atguigu.userprofile.service.UserGroupService;
import com.atguigu.userprofile.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2021-05-04
 */
@RestController
public class UserGroupController
{

    @Autowired
    UserGroupService userGroupService;

    @RequestMapping("/user-group-list")
    @CrossOrigin
    public String getUserGroupList(@RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize) {
        int startNo = (pageNo - 1) * pageSize;
        int endNo = startNo + pageSize;

        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        int count = userGroupService.count(queryWrapper);

        queryWrapper.orderByDesc("id").last(" limit " + startNo + "," + endNo);
        List<UserGroup> userGroupList = userGroupService.list(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail", userGroupList);
        jsonObject.put("total", count);

        return jsonObject.toJSONString();
    }

    @PostMapping(value = "/user-group")
    public Object userGroup(@RequestBody UserGroup userGroup){

        System.out.println(userGroup);

        //1.把分群条件存入Mysql
        userGroupService.saveUserGroupMetaToMysql(userGroup);

        System.out.println("写入以后:"+userGroup);
        //2.计算存入ck
        userGroupService.saveUserGroupToCk(userGroup);

        //3.把数据同步到redis一份，适应 高并发查询的场景
        Integer userGroupNum = userGroupService.saveUserGroupToRedis(userGroup);

        //4.更新mysql中的 user_group_num字段
        userGroup.setUserGroupNum(userGroupNum+0l);
        userGroupService.saveOrUpdate(userGroup);
        //模拟异常 为了让页面卡在 创建分群上
       // int i = 1 /0;

        return "ok";
    }

    @PostMapping(value = "/user-group-evaluate")
    public Object evaluateUserGroupNum(@RequestBody UserGroup userGroup){

        //计算
       return userGroupService.evaluateUserGroupNum(userGroup);
    }

    @PostMapping(value = "/user-group-refresh/{id}")
    public Object updateUserGroup(String busiDate,@PathVariable("id") Long id){

        //重新基于Id和指定的业务日期 计算 分群人数 。
        // 获取完整的分群条件，才能计算。目前只有id和业务日期
        // 查询Mysql，查询指定id的分群的元数据
        UserGroup userGroup = userGroupService.getById(id);
        userGroup.setBusiDate(busiDate);
        userGroup.setTagConditions(JSON.parseArray(userGroup.getConditionJsonStr(),TagCondition.class));

        // 重新计算，更新ck
        userGroupService.updateUserGroup(userGroup);

        // 更新redis
        Integer num = userGroupService.saveUserGroupToRedis(userGroup);

        // 更新Mysql   添加修改日期，更新最新的人数
        userGroup.setUserGroupNum(num+0l);
        userGroup.setUpdateTime(new Date());

        userGroupService.saveOrUpdate(userGroup);

        return "ok";
    }


}

